CKEDITOR.plugins.add('tankCMSimages', {
  requires: ['iframedialog'],
  init : function(editor) {
    CKEDITOR.dialog.addIframe('tankCMSimage_dialog', 'Image', SITE_URL + 'file_upload/ajax_file',800,500)
    editor.addCommand('tankCMSimages', {
      exec:tankCMSimage_onclick
    });
    editor.ui.addButton('tankCMSimages',{
      label:'Upload or insert images from library', 
      command:'tankCMSimages', 
      icon:this.path+'images/icon.png'
    });
    editor.on('selectionChange', function(evt) {
      /*
			 * Despite our initial hope, document.queryCommandEnabled() does not work
			 * for this in Firefox. So we must detect the state by element paths.
			 */
      var command = editor.getCommand('tankCMSimages'),
      element = evt.data.path.lastElement.getAscendant('img', true);

      // If nothing or a valid document
      if ( ! element || (element.getName() == 'img' && element.hasClass('tankCMS-image'))) {
        command.setState(CKEDITOR.TRISTATE_OFF);
      }

      else {
        command.setState(CKEDITOR.TRISTATE_DISABLED);
      }
    });
  }
});

function tankCMSimage_onclick(e) {
  update_instance();
  // run when tankCMS button is clicked]
  CKEDITOR.currentInstance.openDialog('tankCMSimage_dialog')
}